package edu.gatech.oad.yuno;

import android.app.Activity;
import android.widget.RatingBar;
import android.widget.TableRow;
import java.util.Date;
import android.widget.LinearLayout;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity for viewing an activity.
 *
 * @author melaenaroberts
 *
 */
public class ViewTodoActivity extends Activity {//implements View.OnClickListener
	private TextView todoName, todoLocation, todoCategory, todoDescription, todoDate;
	private RatingBar todoPriority;
	private Todo todo;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadEverything();
	}
	
	/**
	 * Loads all of the data from the todo
	 */
	public void loadEverything(){
		setContentView(R.layout.view_item);
		
		long todoId=this.getIntent().getLongExtra("ToDo", 0);
		todo=new Todo(todoId);

		//get the info
		Location loc=todo.getLocation();
		Category cat=todo.getCategory();
		Date dat=todo.getDueDate();
		String des=todo.getDescription();
		int pri=todo.getPriority();
		String name=todo.getName();
		boolean rep=todo.isRecurring();
		
		
		todoName = (TextView) findViewById(R.id.todoName);
		todoName.setText(name);
		
		todoDescription = (TextView) findViewById(R.id.todoDescription);
		if (des != null && !des.equals(""))
			todoDescription.setText(des);
		else{
			todoDescription.setVisibility(todoDescription.GONE);
			TextView descriptionLabel= (TextView) findViewById(R.id.descriptionLabel);
			descriptionLabel.setVisibility(descriptionLabel.GONE);
		}
		
		todoLocation = (TextView) findViewById(R.id.todoLocation);
		if (loc!=null)
			todoLocation.setText(loc.getName());
		else{
			todoLocation.setVisibility(todoLocation.GONE);
			TextView locationLabel=(TextView) findViewById(R.id.locationLabel);
			locationLabel.setVisibility(locationLabel.GONE);
		}
		
		todoCategory = (TextView) findViewById(R.id.todoCategory);
		if (cat!=null){
			todoCategory.setText(cat.getName());
			todoCategory.setTextColor(cat.getColor());
		}
		else{
			todoCategory.setVisibility(todoCategory.GONE);
			TextView categoryLabel=(TextView) findViewById(R.id.categoryLabel);
			categoryLabel.setVisibility(categoryLabel.GONE);
		}
		
		todoDate = (TextView) findViewById(R.id.todoDate);
		if (dat!=null)
			todoDate.setText(DateHelper.format(dat));
		else{
			todoDate.setVisibility(todoDate.GONE);
			TextView completeByLabel=(TextView) findViewById(R.id.completeByLabel);
			completeByLabel.setVisibility(completeByLabel.GONE);
		}
		
		todoPriority=(RatingBar) findViewById(R.id.todoPriority);
		if (pri!=0){
		todoPriority.setNumStars(pri);
		todoPriority.setRating(pri);
		}
		else{
			TextView priorityLabel=(TextView) findViewById(R.id.priorityLabel);
			priorityLabel.setVisibility(priorityLabel.GONE);
			todoPriority.setVisibility(todoPriority.GONE);
		}
        
        if (rep){ //If not repeating and no reminder
        	TextView repeat=(TextView) findViewById(R.id.repeat);
        	repeat.setText("Repeats each week.");
        }
        else{
        	TextView addDetails=(TextView) findViewById(R.id.additionalDetailsLabel);
        	addDetails.setVisibility(addDetails.GONE);
        }
        
        if (false) {//change to boolean for reminder, obviously, which doesn't exist yet
        	TextView reminder=(TextView) findViewById(R.id.reminder);
        	reminder.setText("Reminder requested.");
        }
		
	}
	
	/**
	 * Creates menu.
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.view_item_menu, menu);
        return true;
    }

    /**
     * Takes care of when a menu item is selected. Can edit or delete an item.
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.edit:
            	Intent intent = new Intent(this, AddTodoActivity.class);
            	intent.putExtra("ToDo", todo.getId());
        		startActivity(intent);
                return true;
            case R.id.deleteItem:
            	todo.delete();
            	finish();
            	Toast.makeText(getApplicationContext(), todo.getName() +" deleted.",
    					Toast.LENGTH_SHORT).show();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    public void onResume() {
    	super.onResume();
    	loadEverything();
    }
}